package com.gy.zpchat.ui.grouplist;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import im.zego.zim.entity.ZIMGroupInfo;
import com.gy.zpchat.R;
import com.gy.zpchat.util.GlideUtils;

public class GroupInfoListAdapter extends RecyclerView.Adapter {

    public List<ZIMGroupInfo> groupInfoList = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_info, parent, false);
        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final GroupInfoListAdapter.GroupViewHolder groupViewHolder = (GroupInfoListAdapter.GroupViewHolder) holder;

        ZIMGroupInfo groupInfo = groupInfoList.get(position);
        groupViewHolder.setGroupInfo(groupInfo);

        if (onItemClickListener != null) {
            groupViewHolder.groupListView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, groupInfo);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return groupInfoList.size();
    }

    public void addAll(List<ZIMGroupInfo> list) {
        if (list != null) {
            groupInfoList.addAll(list);
        }
    }

    public void add(ZIMGroupInfo newO) {
        groupInfoList.add(newO);
    }

    public void update(ZIMGroupInfo newO) {
        for (int i = 0; i < groupInfoList.size(); i++) {
            ZIMGroupInfo oldO = groupInfoList.get(i);
            if (oldO.groupID.equals(newO.groupID)) {
                groupInfoList.set(i, newO);
            }
        }
    }

    public ZIMGroupInfo query(String groupID) {
        for (ZIMGroupInfo groupInfo : groupInfoList) {
            if (groupInfo.groupID.equals(groupID)) {
                return groupInfo;
            }
        }
        return null;
    }

    public void delete(ZIMGroupInfo delO) {
        for (ZIMGroupInfo oldO : groupInfoList) {
            if (delO.groupID.equals(oldO.groupID)) {
                groupInfoList.remove(oldO);
                return;
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, ZIMGroupInfo groupInfo);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    public static class GroupViewHolder extends RecyclerView.ViewHolder {

        TextView titleTextView;
        RelativeLayout groupListView;
        ImageView imageViewHeader;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);
            groupListView = itemView.findViewById(R.id.list);
            titleTextView = itemView.findViewById(R.id.title);
            imageViewHeader = itemView.findViewById(R.id.header);
        }

        public void setGroupInfo(ZIMGroupInfo info) {
            if (info.groupName != null && !info.groupName.equals("")) {
                titleTextView.setText(info.groupID + " | " + info.groupName);
            } else {
                titleTextView.setText(info.groupID);
            }
            GlideUtils.loadUrl(imageViewHeader, info.groupAvatarUrl);
        }
    }
}
